using System;
using System.Web.Mvc;
using CSI.Directory.Core.Model;
using CSI.Directory.Web.Filters;
using CSI.Directory.Web.Util;

namespace CSI.Directory.Web.Controllers
{
    public class RegisterController : BaseController
    {


        public ActionResult Index(string type, string returnUrl)
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Go(string firstname, string lastname, string password, string email)
        {
            var t = new TransactionResult() { Successful = false };
            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(email))
                return Json(t); //RedirectToRoute("Controller", new { controller = "Oops" });
            t = UserService.RegisterUser(firstname, lastname, password, email);
            return Json(t);
        }
        public JsonResult UsernameCheck(int id)
        {
            var t = new TransactionResult() { Successful = false };
            var u = UserService.GetUserById(id);
            if (u == null) t.Successful = true;
            return Json(t);
        }

        public JsonResult EmailCheck(string email, string type)
        {
            var t = new TransactionResult() { Successful = false };
            if (string.IsNullOrEmpty(email)) return Json(t);
            if (UserService.GetUserByEmail(email) == null) t.Successful = true;
            return Json(t);
        }

        public ActionResult Package()
        {
            return View("Package");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [RestrictedAction(new[] { "user" },null)]
        public JsonResult Package(string package)
        {
            var t = new TransactionResult() { Successful = false };
            if (!string.IsNullOrEmpty(package))
            {
                Session[SessionNames.PACKAGE1] = package;
                t.ReturnVal = Convert.ToInt32(package);
                t.Successful = true;
            }
            return Json(t);
        }
        [HttpGet]
        [RestrictedAction(new[] { "user" }, null)]
        public ActionResult UpGrade()
        {
            return View("UpGrade");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [RestrictedAction(new[] { "user" },null)]
        public JsonResult UpGrade(string upgrade)
        {
            var t = new TransactionResult() { Successful = false };
            if (!string.IsNullOrEmpty(upgrade))
            {
                Session[SessionNames.PACKAGE2] = upgrade;
                t.Successful = true;
            }
            return Json(t);
        }

        [HttpGet]
        [RestrictedAction(new[] { "user" },null)]
        public ActionResult Confirmation()
        {
            return View("Confirm");
        }

        [HttpGet]
        [RestrictedAction(new[] { "user" },null)]
        public ActionResult Free()
        {
            return View("Free");
        }

        [HttpGet]
        [RestrictedAction(new[] { "user" },null)]
        public ActionResult Payment()
        {
            var total = 0.0;
            switch (Session[SessionNames.PACKAGE1].ToString())
            {
                case "1":
                    total = 150.00;
                    //total = .01;
                    break;
                case "2":
                    //total = .02;
                    total = 300.00;
                    break;
                case "3":
                    //total = .03;
                    total = 600.00;
                    break;
                default:
                    break;
            }
            switch (Session[SessionNames.PACKAGE2].ToString())
            {
                case "1":
                    //total += .01;
                    total += 19.99;
                    break;
                case "2":
                    //total += .02;
                    total += 99.99;
                    break;
                case "3":
                    //total += .03;
                    total += 299.99;
                    break;
                case "4":
                    //total += .04;
                    total += 499.99;
                    break;
                default:
                    break;
            }
            Session["Total"] = total;
            return View("Payment");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [RestrictedAction(new[] { "user" },null)]
        public RedirectToRouteResult FreeAccountComplete()
        {
            UserService.UpdateUser(SessionService.CurrentUser.Id, null, null, null, null, null, "0", "0");
            UserService.AddUserToCRM(SessionService.CurrentUser.Id);
            return RedirectToRoute(new {controller = "Directory", action = "FreeListing"});
            
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [RestrictedAction(new[] { "user" },null)]
        public JsonResult Payment(string firstname, string lastname, string address, string city,
                                         string amount, string state, string zipcode, string phone,  string ccnumber,
                                         string expirationdate)
        {


            var t = TransactionService.ProcessPayment(SessionService.CurrentUser.Id, ccnumber, expirationdate, amount.ToString(),
                                              firstname, lastname, address, city,
                                              state, zipcode, phone);
            if(t.Successful)
                UserService.UpdateUser(SessionService.CurrentUser.Id, address, city, state, zipcode, phone, Session[SessionNames.PACKAGE1].ToString(), Session[SessionNames.PACKAGE2].ToString());
            return Json(t);
        }
        [HttpGet]
        [RestrictedAction(new[] { "user" },null)]
        public ActionResult Receipt()
        {
            return View();
        }
    }

}